package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 通用controller
 */
@Slf4j
@Api(tags = "公共模块接口")
@RestController
@RequestMapping("/admin/common")
public class CommonController {

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        log.info("upload {}", file);
        try {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null) {
                return Result.error("上传文件名为空");
            }
            int index = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(index);
            String filename = UUID.randomUUID() + suffix;
            log.info("filename {}", filename);
            // TODO 文件上传功能
            return Result.success(filename);
        } catch (Exception e) {
            log.error("上传文件异常", e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

}
